﻿/*
 * Author:      Roy Du
 * Url:         http://LeadNT.codeplex.com/
 * Author Url:  http://www.LeadNT.com
 * Title:       LeadNT Personal Library 
 * Copyright:   2011 Roy Du
 * License:     LGPL License
 * LicenseUrl:  http://LeadNT.codeplex.com/license
 * Description: A C# based .NET 3.5 Open-Source ulitity library.
 * Usage: Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;

namespace Com.Leadnt.Library
{
    public class JsHelper
    {
        private static JsHelper _instance = null;

        public static JsHelper Instance
        {
            get {
                if (_instance == null)
                {
                    _instance = new JsHelper();
                }
                return _instance;
            }
        }

        private Page page = null;

        

        /// <summary>
        /// js.alert msg
        /// </summary>
        /// <param name="msg"></param>
        public void Alert(string msg)
        {
            RegisterScript("alert('" + msg.Replace("'", "\'") + "');");
        }

        /// <summary>
        /// js.confirm
        /// </summary>
        /// <param name="msg"></param>
        public void Confirm(string msg)
        {
            RegisterScript("confirm('" + msg.Replace("'", "\'") + "');");
        }

        /// <summary>
        /// js.confirm,include callback functions
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="yesFunc"></param>
        /// <param name="noFunc"></param>
        public void Confirm(string msg, string yesFunc, string noFunc)
        {
            RegisterScript("if(confirm('" + msg.Replace("'", "\'") + "')){" + yesFunc + "}else{" + noFunc + "}");
        }

        /// <summary>
        /// js.your custom script
        /// </summary>
        /// <param name="script"></param>
        public void CustomScript(string script)
        {
            RegisterScript(script);
        }

        private void RegisterScript(string script)
        {
            StringBuilder scriptBuilder = new StringBuilder();
            scriptBuilder.AppendLine("<script type=\"text/javascript\">");
            scriptBuilder.AppendLine("//<![CDATA[");
            scriptBuilder.AppendLine(script);
            scriptBuilder.AppendLine("//]]>");
            scriptBuilder.AppendLine("</script>");
			try
            {
                page = (Page)HttpContext.Current.Handler;
            }
            catch { }
			
            if (page != null)
            {
                page.ClientScript.RegisterClientScriptBlock(page.GetType(),
                    string.Format("script_{0}", DateTime.Now.ToLongTimeString()),
                    scriptBuilder.ToString());
            }
            else
            {
                try
                {
                    HttpContext.Current.Response.Write(script);
                }
                catch { }
            }
        }
    }
}
